package elicecn.data.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;

public class Flight {
	private static final String SINA_URL = "http://php.weather.sina.com.cn/flight/";
	// 查询航班号
	private static final String PARAM_FNO = "fno";
	// 查询两地航班
	private static final String PARAM_FROMCITYNAME = "fromCityName";
	private static final String PARAM_TOCITYNAME = "toCityName";
	private static final String PARAM_AIRLINES = "Airlines";
	// 数据结构
	// 参数param
	// ..|-查询参数fno/fromCityName/toCityName/Airlines
	// 航班列表resultList
	// ..|-航班数据
	public static final String PARAM = "param";
	public static final String RESULT_LIST = "resultList";
	// 航班数据
	public static final String FLIGHT_CODE = "flightCode";// 航班号
	public static final String FROM_CITY = "fromCity";// 出发地
	public static final String FROM_CITY_STATION = "fromCityStation";// 候机楼
	public static final String TO_CITY = "toCity";// 到达地
	public static final String TO_CITY_STATION = "toCityStation";// 接机楼
	public static final String DEP_TIME = "depTime";// 预计起飞
	public static final String ACTUAL_DEP_TIME = "actualDepTime";// 实际起飞
	public static final String ARR_TIME = "arrTime";// 预计到达
	public static final String ACTUAL_ARR_TIME = "actualArrTime";// 实际到达
	public static final String STATUS = "status";// 状态
	public static final String AIRLINE = "airline";// 航空公司
	public static final String AIRLINE_LOGO = "airlineLogo";// 航空公司图标

	/**
	 * 根据航班号查询航班动态
	 * 
	 * @param flightNo
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static Map<String, Object> flight(String flightNo)
			throws ClientProtocolException, IOException {
		Map<String, String> paramMap = new HashMap<String, String>(0);
		paramMap.put(PARAM_FNO, flightNo);
		String html = Request.httpPost(SINA_URL, paramMap, "GBK");
		List<Map<String, Object>> resultList = parseFlightHtml(html);
		// 输出
		Map<String, Object> resultMap = new HashMap<String, Object>(0);
		resultMap.put(PARAM, paramMap);
		resultMap.put(RESULT_LIST, resultList);
		return resultMap;
	}

	/**
	 * 根据出发城市、到达城市、航空公司查询航班动态
	 * 
	 * @param fromCity
	 * @param toCity
	 * @param airlines
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static Map<String, Object> flight(String fromCity, String toCity,
			String airlines) throws ClientProtocolException, IOException {
		Map<String, String> paramMap = new HashMap<String, String>(0);
		paramMap.put(PARAM_FROMCITYNAME, fromCity);
		paramMap.put(PARAM_TOCITYNAME, toCity);
		paramMap.put(PARAM_AIRLINES, airlines);
		String html = Request.httpPost(SINA_URL, paramMap, "GBK");
		List<Map<String, Object>> resultList = parseFlightHtml(html);
		// 输出
		Map<String, Object> resultMap = new HashMap<String, Object>(0);
		resultMap.put(PARAM, paramMap);
		resultMap.put(RESULT_LIST, resultList);
		return resultMap;
	}

	// 分析网页返回结果集
	private static List<Map<String, Object>> parseFlightHtml(
			final String htmlContent) {
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>(
				0);
		try {
			String[] htmlRow = parseFlightHtmlData(htmlContent);
			for (String html : htmlRow) {
				Map<String, Object> rowMap = parseFlightHtmlRowData(html);
				resultList.add(rowMap);
			}
		} catch (Exception e) {
			// 无结果集则会报异常
			e.printStackTrace();
		}
		return resultList;
	}

	// 截取数据行内容
	private static String[] parseFlightHtmlData(final String htmlContent) {
		String html = htmlContent;
		String filter2 = "<tbody>";
		String filter3 = "</tbody>";
		String filter4 = "<tr>";
		String filter5 = "</tr>";
		int idx = html.indexOf(filter2);
		html = html.substring(idx + filter2.length());
		idx = html.indexOf(filter3);
		html = html.substring(0, idx);
		idx = html.indexOf(filter4);
		html = html.substring(idx + filter4.length());
		idx = html.lastIndexOf(filter5);
		html = html.substring(0, idx);
		String[] result = html.split(filter5 + filter4);
		return result;
	}

	// 分析航班数据
	private static Map<String, Object> parseFlightHtmlRowData(
			final String htmlContent) {
		Map<String, Object> rowMap = new HashMap<String, Object>(0);
		String flightNo = parseFlightDataFlightNo(htmlContent);
		String fromCity = parseFlightDataFromCity(htmlContent);
		String fromCityStation = parseFlightDataFromCityStation(htmlContent);
		String toCity = parseFlightDataToCity(htmlContent);
		String toCityStation = parseFlightDataToCityStation(htmlContent);
		String depTime = parseFlightDataDepTime(htmlContent);
		String actualDepTime = parseFlightDataActualDepTime(htmlContent);
		String arrTime = parseFlightDataArrTime(htmlContent);
		String actualArrTime = parseFlightDataActualArrTime(htmlContent);
		String status = parseFlightDataStatus(htmlContent);
		String airline = parseFlightDataAirline(htmlContent);
		String airlineLogo = parseFlightDataAirlineLogo(htmlContent);
		rowMap.put(FLIGHT_CODE, flightNo);
		rowMap.put(FROM_CITY, fromCity);
		rowMap.put(FROM_CITY_STATION, fromCityStation);
		rowMap.put(TO_CITY, toCity);
		rowMap.put(TO_CITY_STATION, toCityStation);
		rowMap.put(DEP_TIME, depTime);
		rowMap.put(ACTUAL_DEP_TIME, actualDepTime);
		rowMap.put(ARR_TIME, arrTime);
		rowMap.put(ACTUAL_ARR_TIME, actualArrTime);
		rowMap.put(STATUS, status);
		rowMap.put(AIRLINE, airline);
		rowMap.put(AIRLINE_LOGO, airlineLogo);
		return rowMap;
	}

	// 航班号
	private static String parseFlightDataFlightNo(final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</a>";
		String filter2 = ">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "");
	}

	// 出发城市
	private static String parseFlightDataFromCity(final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</td>";
		String filter2 = ">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "");
	}

	// 送机楼
	private static String parseFlightDataFromCityStation(
			final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</td>";
		String filter2 = ">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "");
	}

	// 到达城市
	private static String parseFlightDataToCity(final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</td>";
		String filter2 = ">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "");
	}

	// 接机楼
	private static String parseFlightDataToCityStation(final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</td>";
		String filter2 = ">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "");
	}

	// 计划起飞时间
	private static String parseFlightDataDepTime(final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</td>";
		String filter2 = ">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "");
	}

	// 实际起飞时间
	private static String parseFlightDataActualDepTime(final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</td>";
		String filter2 = ">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "");
	}

	// 计划到达时间
	private static String parseFlightDataArrTime(final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</td>";
		String filter2 = ">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "");
	}

	// 实际到达时间
	private static String parseFlightDataActualArrTime(final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</td>";
		String filter2 = ">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "");
	}

	// 当前状态
	private static String parseFlightDataStatus(final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</td>";
		String filter2 = "<td align=\"center\">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "").replaceAll(
				"<[\\W\\w]*>([\\S| ]*)<[\\W\\w]*>", "$1");
	}

	// 航空公司
	private static String parseFlightDataAirline(final String htmlContent) {
		String html = htmlContent;
		String filter1 = "</td>";
		String filter2 = ">";
		int idx = html.indexOf(filter1);
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		idx = html.indexOf(filter1, idx + filter1.length());
		html = html.substring(0, idx);
		idx = html.lastIndexOf(filter2);
		html = html.substring(idx + filter2.length());
		return html.replaceAll("&nbsp;", "");
	}

	// 航空公司徽标
	private static String parseFlightDataAirlineLogo(final String htmlContent) {
		String html = htmlContent;
		String filter2 = "<img src=\"";
		String filter3 = "\"";
		int idx = html.indexOf(filter2);
		html = html.substring(idx + filter2.length());
		idx = html.indexOf(filter3);
		html = html.substring(0, idx);
		return html.replaceAll("&nbsp;", "");
	}

	public static void main(String[] args) throws ClientProtocolException,
			IOException {
		Map<String, Object> resultMap = null;
		resultMap = flight("MU9362");
		System.out.println(new JSONObject(resultMap));
		resultMap = flight("武汉", "成都", "");
		System.out.println(new JSONObject(resultMap));
	}

}
